﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Aufgabe_6_Planets
{
    class EnglertMichaelSonne : DrawableGameComponent
    {

        private Model sun;
        private ContentManager content;
        private Game game;
        private Matrix sunMatrix, viewMatrix, projectionMatrix, worldMatrix;
        private Effect e;

        public EnglertMichaelSonne(Game game)
            : base(game)
        { 
            this.WorldMatrix = Matrix.Identity;
            this.game = game;
        }


        public Matrix WorldMatrix
        { get; set; }

        public Matrix ViewMatrix
        { get; set; }

        public Matrix ProjectionMatrix
        { get; set; }


        public override void Initialize()
        {
            this.content = this.game.Content;
            base.Initialize();
        }


        protected override void LoadContent()
        {
            this.sun = content.Load<Model>("models\\EnglertMichaelModelSun");

            e = content.Load<Effect>("effects\\EnglertMichaelCartoon");

            foreach (ModelMesh mesh in sun.Meshes)
            {
                foreach (ModelMeshPart effect in mesh.MeshParts)
                {
                    effect.Effect = e;
                }
            }

            base.LoadContent();
        }


        public override void Update(GameTime gameTime)
        {

            sunMatrix = Matrix.Identity;

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {

            // Mond zeichnen
            foreach (ModelMesh mesh in sun.Meshes)
            {
                foreach (ModelMeshPart effect in mesh.MeshParts)
                {
                    effect.Effect.Parameters["matViewProjection"].SetValue(sunMatrix * WorldMatrix * ViewMatrix * ProjectionMatrix);
                    effect.Effect.Parameters["matView"].SetValue(sunMatrix * WorldMatrix);
                    effect.Effect.Parameters["fvEyePosition"].SetValue(new Vector3(0, 0, -100));
                    effect.Effect.Parameters["fvLightPosition"].SetValue(new Vector3(-100, 100, 20));
                    foreach (EffectPass pass in effect.Effect.CurrentTechnique.Passes)
                    {
                        pass.Apply();
                    }
                }

                mesh.Draw();
            }



            base.Draw(gameTime);
        }
    }
}
